<?php
namespace app\authorization\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use tree\Tree;
use app\admin\model\AdminMenuModel;
use app\admin\model\AccessModel;
use app\authorization\model\RbacMenuModel as RMM;
use think\Validate;
/**
 * 20200415
 * 权限版本管理
 */
class RbacMenuVersionController extends AdminBaseController
{	
	private $rbacMenuModel;
	public function _initialize()
	{
		$this->rbacMenuModel = new RMM();
	}
	/**
	 * 权限菜单首页
	 */
	public function index ()
	{
		//->where('create_time' , '>=' , "1587095262")
		$list = Db('access_versions')->select();
		$this->assign( 'list' , $list );
		return $this->fetch( 'rbac_menu/rbac_version' );
	}

	/**
	 * 添加版本
	 */
	public  function add()
	{
		if($this->request->isPost()){
			$data=$this->request->param();
			$rule=[
				'name'=>'require|max:200',
				'price'=>'require'
			];
			$msg=[
				'name.require'=>'版本名称不能为空',
				'name.max'=>'版本名称不能超过200个字符',
				'price.require'=>'价格不能为空'
			];
			foreach ($data as $key => $value) {
				$data[$key]=trim($value);
			}
			$validate= new Validate($rule,$msg);
			//数据验证
			if(!$validate->check($data)){
				$this->error($validate->getError());
			}
			//存入数据库
			if(isset($data['edit'])){//更新
				unset($data['edit']);
				$res=Db::name('access_versions')->where('id',$data['id'])->update($data);
				$msg=$res?"更新成功!":"更新失败";
			}else{//添加
				$data['create_time']=time();
				$res=Db::name('access_versions')->insert($data);
				$msg=$res?"新增成功!":"新增失败";
			}
			if($res){
				return zy_json_echo( true , $msg );
			}else{
				//$this->error($msg.'，请检查或稍后再试！');
				return zy_json_echo( false , $msg.'请检查或稍后再试!' );
			}
		}else{
			$data=$this->request->param();
			$action=null;
			if(isset($data['id'])){//修改
				$action='edit';
				$v=Db::name('access_versions')->where('id',$data['id'])->find();
				$this->assign('data',$v);
			}
			$this->assign('action',$action);
			return $this->fetch( 'rbac_menu/add_version' );
		}
	}

	/**
	 * 权限版本删除
	 */
	public function delete()
	{
		$id=$this->request->param('id');
		//查询是否有被引用，否则不能删除
		//待实现
		//删除授权菜单表中的数据
		$res=$this->clear_access_menu($id);
		if($res){
			$res=Db::name('access_versions')->where('id',$id)->delete();
		}
		if($res){
			//$this->success('删除成功！');
			return zy_json_echo( true , '删除成功!');
		}else{
			return zy_json_echo( false , '删除失败，请稍后再试！');
		}
	}

	/**
	 * 清除版本授权菜单
	 * @param  版本id
	 * @return true   false
	 */
	private function clear_access_menu($id)
	{
		Db::name('access_versions_menu')->where('access_versions_id',$id)->delete();
		Db::name('auth_access')->where('access_versions_id',$id)->delete();
		return true;
	}
	

	/**
	 * 设置基础菜单
	 */
	public function setBaseMenu( $versionId )
	{

		$list = $this->rbacMenuModel->getBaseMenu( $versionId );
		$this->assign( 'list' , json_encode( $list , JSON_UNESCAPED_UNICODE ) );
		return $this->fetch( 'rbac_menu/set_base_menu' );
	}

	/**
	 * 版本菜单提交
	 */
	public function baseMenuPost( )
	{
		if( $this->request->isPost() ){
			$param = $this->request->param();
			$versionId = $param [ 'versionId' ];
			$updateList = isset( $param [ 'list' ] ) ? $param ['list'] : [] ; 
			$res = $this->rbacMenuModel->updateVersions( $versionId , $updateList );
			if( !$res ){
				return zy_json_echo( false , '保存失败,请稍后再试!' );
			}
			return zy_json_echo( true , '保存成功!' );
		}else{
			return zy_json_echo( false , '请求类型错误');
		}
	}
}